import dayjs from '../../../utils/dayjs.min'

Component({

  /**
   * 组件的属性列表
   */
  properties: {
    startDate:{
      type:String,
      value:'',
      observer:function(newVal,oldVal){
        this.setData({
          startDate:newVal
        })
      }
    },
    endDate:{
      type:String,
      value:'',
      observer:function(newVal,oldVal){
        this.setData({
          endDate:newVal
        })
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    type: 1,
    startDate: '',
    endDate: '',
    currentDate: new Date().getTime(),
    maxDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
  },
  created() {},
  /**
   * 组件的方法列表
   */
  methods: {
    // 打开选择器
    open1() {
      this.setData({
        show: true,
        type: 1
      })
    },
    open2() {
      this.setData({
        show: true,
        type: 2
      })
    },
    // 选择器确认事件
    confirm(event) {
      if (this.data.type == 1) {
        this.setData({
          startDate: dayjs(event.detail).format('YYYY-MM-DD')
        })
      } else {
        this.setData({
          endDate: dayjs(event.detail).format('YYYY-MM-DD')
        })
      }
      this.triggerEvent('change', {
        startDate: this.data.startDate,
        endDate: this.data.endDate
      })
      this.colse()
    },
    // 关闭选择器
    colse() {
      this.setData({
        show: false
      })
    },
    clear(){
      this.setData({
        startDate: '',
        endDate:''
      })
    }
  }
})